//
//  AddMaterialViewController.m
//  Article
//
//  Created by LONG on 16/4/21.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "AddMaterialViewController.h"
#import "CaptureViewController.h"

@interface AddMaterialViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITextFieldDelegate,UIAlertViewDelegate,PassImageDelegate>
@property (weak, nonatomic) IBOutlet UITextField *paiXutextfiled;

@property (weak, nonatomic) IBOutlet UIButton *tongLBut;
//@property (weak, nonatomic) IBOutlet UIButton *tuWBut;
//@property (weak, nonatomic) IBOutlet UIButton *mingPBut;
//@property (weak, nonatomic) IBOutlet UIButton *erWMBut;

@property (weak, nonatomic) IBOutlet UIButton *cutOutBut;

@property (nonatomic,strong)NSMutableArray *arrBut;

@property (nonatomic,strong)UIView *tongLView;
//@property (nonatomic,strong)UIView *tuWView;
//@property (nonatomic,strong)UIView *mingPView;
//@property (nonatomic,strong)UIView *erWMView;

@end

@implementation AddMaterialViewController{
    UIActionSheet * _action;
    UIImagePickerController *_myPicker;
    NSString *_type;//类型
    NSString *_imageurlstr;//图片str
    NSString *_ad_top;//顶部
    NSString *_ad_bottom;//底部
    NSString *_ad_cut;//剪裁
    NSString *_ad_default;//默认
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"添加素材";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    [self.navigationItem.leftBarButtonItem setTintColor:UIColorFromRGB(0xffffff)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(addview)];
    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:UIColorFromRGB(0xffffff)} forState:UIControlStateNormal];
    
    _myPicker = [[UIImagePickerController alloc]init];
    _myPicker.view.backgroundColor = [UIColor clearColor];
    _myPicker.allowsEditing = YES;
    _myPicker.delegate = self;
    _myPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    _myPicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    
    _arrBut = [[NSMutableArray alloc]init];
    [_arrBut addObject:_tongLBut];
    //[_arrBut addObject:_tuWBut];
    //[_arrBut addObject:_mingPBut];
   // [_arrBut addObject:_erWMBut];
    _paiXutextfiled.keyboardType = UIKeyboardTypeNumberPad;
    
    //添加素材
    if (self.pushint==1) {
        self.cutOutBut.hidden = YES;
        [self tongLView];
        _type = @"banner";
        _ad_top = @"1";
        _ad_bottom = @"1";
        _ad_cut = @"0";
        _ad_default = @"1";
    }
    //编辑素材
    else{
        self.navigationItem.title = @"编辑素材";
        _ad_top = @"1";
        _ad_bottom = @"1";
        _ad_cut = @"0";
        _ad_default = @"1";
        
       // _tuWBut.hidden = YES;
       // _mingPBut.hidden = YES;
       // _erWMBut.hidden = YES;
        [_tongLBut setTitle:self.setTitle forState:UIControlStateNormal];
        _tongLBut.userInteractionEnabled = NO;
        
        if ([self.setTitle isEqualToString:@"通栏"]){
            [self tongLView];
            _type = @"banner";
        }
//        else if ([self.setTitle isEqualToString:@"图文"]){
//            [self tuWView];
//            _type = @"imgtext";
//        }else if ([self.setTitle isEqualToString:@"名片"]){
//            [self mingPView];
//            _type = @"vcard";
//        }
//        else if ([self.setTitle isEqualToString:@"二维码"]){
//            [self erWMView];
//            _type = @"tdcode";
//        }
        
    }
    
}
- (void)addview{
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    if ([_type isEqualToString:@"banner"]) {
        
        UITextField *ad_link = (id)[self.view viewWithTag:130];
        
        [dic setValue:@"banner" forKey:@"type"];
        [dic setValue:[NSString stringWithFormat:@"%@",_imageurlstr] forKey:@"ad_img"];
        [dic setValue:[NSString stringWithFormat:@"%@",ad_link.text] forKey:@"ad_link"];
        
        
    }else if ([_type isEqualToString:@"imgtext"]){
        
        UITextField *ad_pro_title = (id)[self.view viewWithTag:131];
        UITextField *ad_pro_des = (id)[self.view viewWithTag:132];
        UITextField *ad_link = (id)[self.view viewWithTag:133];
        
        [dic setValue:@"imgtext" forKey:@"type"];
        [dic setValue:[NSString stringWithFormat:@"%@",_imageurlstr] forKey:@"ad_img"];
        [dic setValue:[NSString stringWithFormat:@"%@",ad_pro_title.text] forKey:@"ad_pro_title"];
        [dic setValue:[NSString stringWithFormat:@"%@",ad_pro_des.text] forKey:@"ad_pro_des"];
        [dic setValue:[NSString stringWithFormat:@"%@",ad_link.text] forKey:@"ad_link"];
        
    }else if ([_type isEqualToString:@"vcard"]){
        
        UITextField *ad_name = (id)[self.view viewWithTag:134];
        UITextField *ad_mobile = (id)[self.view viewWithTag:135];
        UITextField *ad_address = (id)[self.view viewWithTag:136];
        
        [dic setValue:@"vcard" forKey:@"type"];
        [dic setValue:[NSString stringWithFormat:@"%@",_imageurlstr] forKey:@"ad_head"];
        [dic setValue:[NSString stringWithFormat:@"%@",ad_name.text] forKey:@"ad_name"];
        [dic setValue:[NSString stringWithFormat:@"%@",ad_mobile.text] forKey:@"ad_mobile"];
        [dic setValue:[NSString stringWithFormat:@"%@",ad_address.text] forKey:@"ad_address"];
        
        
    }else if ([_type isEqualToString:@"tdcode"]){
        
        UITextField *ad_code_title = (id)[self.view viewWithTag:137];
        
        [dic setValue:@"tdcode" forKey:@"type"];
        [dic setValue:[NSString stringWithFormat:@"%@",_imageurlstr] forKey:@"ad_code_img"];
        [dic setValue:[NSString stringWithFormat:@"%@",ad_code_title.text] forKey:@"ad_code_title"];
        [dic setValue:@"null" forKey:@"ad_code_des"];
        
    }
    
    [dic setValue:_ad_top forKey:@"ad_top"];
    [dic setValue:_ad_bottom forKey:@"ad_bottom"];
    [dic setValue:_ad_cut forKey:@"ad_cut"];
    [dic setValue:_ad_default forKey:@"ad_default"];
    [dic setValue:[NSString stringWithFormat:@"%@",_paiXutextfiled.text] forKey:@"sort"];
    [dic setValue:[user objectForKey:@"token"] forKey:@"user_token"];
    //添加素材
    if (!(self.pushint==1)) {
        [dic setValue:[NSString stringWithFormat:@"%@",_ads_id] forKey:@"ads_id"];
        
    }
    NSLog(@"%@",dic);
    AFHTTPRequestOperationManager *manager = [MyTools initAFHttpManager];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [manager POST:ADDADS_URL parameters:dic success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if ([[responseObject objectForKey:@"status"] intValue] == 1) {
            [self.view makeToast:[responseObject objectForKey:@"info"] duration:1.0 position:CSToastPositionCenter];
            [self.navigationController popViewControllerAnimated:YES];
            
        }else{
            [self.view makeToast:[responseObject objectForKey:@"info"] duration:1.0 position:CSToastPositionCenter];
        }
        NSLog(@"添加广告%@",[responseObject objectForKey:@"info"] );
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        if ([MyTools connectedToNetwork] == 0) {
            [self.view makeToast:@"请检查是否联网" duration:1.0 position:CSToastPositionCenter];
        }else{
            [self.view makeToast:@"服务器错误，再试一次" duration:1.0 position:CSToastPositionCenter];
        }
    }];
    
}
- (IBAction)typeBut:(UIButton *)sender {
    for (UIButton *but in _arrBut) {
        [but setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
        [but setBackgroundColor:UIColorFromRGB(0xffffff)];
    }
    [sender setTitleColor:UIColorFromRGB(0xffffff) forState:UIControlStateNormal];
    [sender setBackgroundColor:UIColorFromRGB(MYColor)];
    
    if (_tongLView) {
        [_tongLView removeFromSuperview];
        _tongLView = nil;
    }
//    if (_mingPView){
//        [_mingPView removeFromSuperview];
//        _mingPView = nil;
//    }
//    if (_tuWView){
//        [_tuWView removeFromSuperview];
//        _tuWView = nil;
//    }
//    if(_erWMView){
//        [_erWMView removeFromSuperview];
//        _erWMView = nil;
//    }
    
    switch (sender.tag) {
        case 70:
            [self tongLView];
            _type = @"banner";
            
            break;
//        case 71:
//            [self tuWView];
//            _type = @"imgtext";
//            
//            break;
//        case 72:
//            [self mingPView];
//            _type = @"vcard";
//            
//            break;
//        case 73:
//            [self erWMView];
//            _type = @"tdcode";
//            
//            break;
        default:
            break;
    }
}
//顶部
- (IBAction)upbut:(UIButton *)sender {
    if (sender.selected) {
        sender.selected = NO;
        _ad_top = @"1";
        [sender setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateSelected];
    }else{
        sender.selected = YES;
        _ad_top = @"0";
        [sender setImage:[UIImage imageNamed:@"未选中"] forState:UIControlStateSelected];
    }
}
//底部
- (IBAction)nextBut:(UIButton *)sender {
    
    if (sender.selected) {
        sender.selected = NO;
        _ad_bottom = @"1";
        [sender setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateSelected];
    }else{
        sender.selected = YES;
        _ad_bottom = @"0";
        [sender setImage:[UIImage imageNamed:@"未选中"] forState:UIControlStateSelected];
    }
}
//裁剪
- (IBAction)tailorBut:(UIButton *)sender {
    
    if (sender.selected) {
        sender.selected = NO;
        _ad_cut = @"0";
        [sender setImage:[UIImage imageNamed:@"未选中"] forState:UIControlStateSelected];
    }else{
        sender.selected = YES;
        _ad_cut = @"1";
        [sender setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateSelected];
    }
}
//默认
- (IBAction)moRenBut:(UIButton *)sender {
    if (sender.selected) {
        sender.selected = NO;
        _ad_default = @"1";
        [sender setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateSelected];
    }else{
        sender.selected = YES;
        _ad_default = @"0";
        [sender setImage:[UIImage imageNamed:@"未选中"] forState:UIControlStateSelected];
    }
}

- (UIView *)tongLView{
    if (_tongLView == nil) {
        _tongLView = [[UIView alloc]init];
        _tongLView.frame = CGRectMake(0, 126, WIDTH, 200);
        [self.view addSubview:_tongLView];
        
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, WIDTH-20, 80)];
        if ([_dataDic objectForKey:@"ad_img"]) {
            [image sd_setImageWithURL:[NSURL URLWithString:[_dataDic objectForKey:@"ad_img"]]];
        }else{
            image.image = [UIImage imageNamed:@"tonglan"];
        }
        image.userInteractionEnabled = YES;
        UITapGestureRecognizer *singleTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonpress:)];
        [image addGestureRecognizer:singleTap1];
        image.tag = 91;
        [_tongLView addSubview:image];
        
        UIImageView *urlimage = [[UIImageView alloc]initWithFrame:CGRectMake(15, 103, 20, 20)];
        urlimage.image = [UIImage imageNamed:@"Unknown"];
        [_tongLView addSubview:urlimage];
        
        UITextField *textview = [[UITextField alloc]initWithFrame:CGRectMake(40, 100, WIDTH-50, 25)];
        textview.tag = 130;
        textview.layer.masksToBounds = YES;
        textview.layer.cornerRadius = 6.0;
        textview.layer.borderWidth = 1;
        textview.layer.borderColor = [UIColorFromRGB(0xefeff1) CGColor];
        textview.placeholder = @" 请输入你的微店或移动站点的URL";
        if ([_dataDic objectForKey:@"ad_link"]) {
            textview.text = [_dataDic objectForKey:@"ad_link"];
        }
        textview.font = [UIFont fontWithName:@"Heiti SC" size:12];;
        [_tongLView addSubview:textview];
        
        UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(10, 135, WIDTH-20, 40)];
        lab.text = @"注:可制作680*120像素图片,链接店铺地址或产品单页等.";
        lab.font = [UIFont fontWithName:@"Heiti SC" size:11];;
        lab.numberOfLines = 0;
        [_tongLView addSubview:lab];
    }
    return _tongLView;
}
//- (UIView *)tuWView{
//    if (_tuWView == nil) {
//        _tuWView = [[UIView alloc]init];
//        _tuWView.frame = CGRectMake(0, 126, WIDTH, 200);
//        [self.view addSubview:_tuWView];
//        
//        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(20, 10, 80, 80)];
//        if ([_dataDic objectForKey:@"ad_img"]) {
//            [image sd_setImageWithURL:[NSURL URLWithString:[_dataDic objectForKey:@"ad_img"]]];
//        }else{
//            image.image = [UIImage imageNamed:@"tuwen"];
//        }
//        image.userInteractionEnabled = YES;
//        UITapGestureRecognizer *singleTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonpress:)];
//        [image addGestureRecognizer:singleTap1];
//        image.tag = 91;
//        [_tuWView addSubview:image];
//        
//        UITextField *nametextview = [[UITextField alloc]initWithFrame:CGRectMake(110, 20, WIDTH-140, 25)];
//        nametextview.tag = 131;
//        nametextview.layer.masksToBounds = YES;
//        nametextview.layer.cornerRadius = 6.0;
//        nametextview.layer.borderWidth = 1;
//        nametextview.layer.borderColor = [UIColorFromRGB(0xefeff1) CGColor];
//        nametextview.placeholder = @" 请输入产品或服务名称";
//        if ([_dataDic objectForKey:@"ad_pro_title"]) {
//            nametextview.text = [_dataDic objectForKey:@"ad_pro_title"];
//        }
//        nametextview.font = [UIFont fontWithName:@"Heiti SC" size:12];;
//        [_tuWView addSubview:nametextview];
//        
//        UITextField *desctextview = [[UITextField alloc]initWithFrame:CGRectMake(110, 50, WIDTH-140, 25)];
//        desctextview.tag = 132;
//        desctextview.layer.masksToBounds = YES;
//        desctextview.layer.cornerRadius = 6.0;
//        desctextview.layer.borderWidth = 1;
//        desctextview.layer.borderColor = [UIColorFromRGB(0xefeff1) CGColor];
//        desctextview.placeholder = @" 请输入产品或描述名称";
//        desctextview.font = [UIFont fontWithName:@"Heiti SC" size:12];
//        if ([_dataDic objectForKey:@"ad_pro_des"]) {
//            desctextview.text = [_dataDic objectForKey:@"ad_pro_des"];
//        }
//        [_tuWView addSubview:desctextview];
//        
//        
//        UIImageView *urlimage = [[UIImageView alloc]initWithFrame:CGRectMake(15, 103, 20, 20)];
//        urlimage.image = [UIImage imageNamed:@"Unknown"];
//        [_tuWView addSubview:urlimage];
//        
//        UITextField *textview = [[UITextField alloc]initWithFrame:CGRectMake(40, 100, WIDTH-50, 25)];
//        textview.tag = 133;
//        textview.layer.masksToBounds = YES;
//        textview.layer.cornerRadius = 6.0;
//        textview.layer.borderWidth = 1;
//        textview.layer.borderColor = [UIColorFromRGB(0xefeff1) CGColor];
//        textview.placeholder = @" 请输入你的微店或移动站点的URL";
//        if ([_dataDic objectForKey:@"ad_link"]) {
//            textview.text = [_dataDic objectForKey:@"ad_link"];
//        }
//        textview.font = [UIFont fontWithName:@"Heiti SC" size:12];;
//        [_tuWView addSubview:textview];
//        
//        UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(10, 135, WIDTH-20, 40)];
//        lab.text = @"注:产品图或LOGO,链接店铺地址或产品单页等.";
//        lab.font = [UIFont fontWithName:@"Heiti SC" size:11];;
//        lab.numberOfLines = 0;
//        [_tuWView addSubview:lab];
//        
//    }
//    return _tuWView;
//}
//- (UIView *)mingPView{
//    if (_mingPView == nil) {
//        _mingPView = [[UIView alloc]init];
//        _mingPView.frame = CGRectMake(0, 126, WIDTH, 200);
//        [self.view addSubview:_mingPView];
//        
//        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(20, 15, 80, 80)];
//        if ([_dataDic objectForKey:@"ad_head"]) {
//            [image sd_setImageWithURL:[NSURL URLWithString:[_dataDic objectForKey:@"ad_head"]]];
//        }else{
//            image.image = [UIImage imageNamed:@"tuwen"];
//        }
//        image.userInteractionEnabled = YES;
//        UITapGestureRecognizer *singleTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonpress:)];
//        [image addGestureRecognizer:singleTap1];
//        image.tag = 91;
//        [_mingPView addSubview:image];
//        
//        UITextField *nametextview = [[UITextField alloc]initWithFrame:CGRectMake(110, 10, WIDTH-200, 25)];
//        nametextview.tag = 134;
//        nametextview.layer.masksToBounds = YES;
//        nametextview.layer.cornerRadius = 6.0;
//        nametextview.layer.borderWidth = 1;
//        nametextview.layer.borderColor = [UIColorFromRGB(0xefeff1) CGColor];
//        nametextview.placeholder = @" 请输入姓名";
//        if ([_dataDic objectForKey:@"ad_name"]) {
//            nametextview.text = [_dataDic objectForKey:@"ad_name"];
//        }
//        nametextview.font = [UIFont fontWithName:@"Heiti SC" size:12];;
//        [_mingPView addSubview:nametextview];
//        
//        UITextField *desctextview = [[UITextField alloc]initWithFrame:CGRectMake(110, 40, WIDTH-200, 25)];
//        desctextview.tag = 135;
//        desctextview.layer.masksToBounds = YES;
//        desctextview.layer.cornerRadius = 6.0;
//        desctextview.layer.borderWidth = 1;
//        desctextview.layer.borderColor = [UIColorFromRGB(0xefeff1) CGColor];
//        desctextview.placeholder = @" 请输入手机号";
//        if ([_dataDic objectForKey:@"ad_mobile"]) {
//            desctextview.text = [_dataDic objectForKey:@"ad_mobile"];
//        }
//        desctextview.font = [UIFont fontWithName:@"Heiti SC" size:12];;
//        [_mingPView addSubview:desctextview];
//        
//        UIButton *fhonebut = [[UIButton alloc]initWithFrame:CGRectMake(WIDTH-80, 40, 70, 25)];
//        fhonebut.titleLabel.font = [UIFont fontWithName:@"Heiti SC" size:12];
//        [fhonebut setTitle:@"一键拨号" forState:UIControlStateNormal];
//        fhonebut.layer.cornerRadius = 6;
//        fhonebut.backgroundColor = UIColorFromRGB(MYColor);
//        [fhonebut addTarget:self action:@selector(phontbut) forControlEvents:UIControlEventTouchUpInside];
//        [_mingPView addSubview:fhonebut];
//        
//        UITextField *textview = [[UITextField alloc]initWithFrame:CGRectMake(110, 70, WIDTH-200, 25)];
//        textview.tag = 136;
//        textview.layer.masksToBounds = YES;
//        textview.layer.cornerRadius = 6.0;
//        textview.layer.borderWidth = 1;
//        textview.layer.borderColor = [UIColorFromRGB(0xefeff1) CGColor];
//        textview.placeholder = @" 请输入你的联系地址";
//        if ([_dataDic objectForKey:@"ad_address"]) {
//            textview.text = [_dataDic objectForKey:@"ad_address"];
//        }
//        textview.font = [UIFont fontWithName:@"Heiti SC" size:12];;
//        [_mingPView addSubview:textview];
//        
//        UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(10, 110, WIDTH-20, 40)];
//        lab.text = @"注:头像或LOGO及联系方式等,支持一键拨号.";
//        lab.font = [UIFont fontWithName:@"Heiti SC" size:11];;
//        lab.numberOfLines = 0;
//        [_mingPView addSubview:lab];
//    }
//    return _mingPView;
//}
//- (void)phontbut{
//    UITextField *ad_mobile = (id)[self.view viewWithTag:135];
//    
//    if (ad_mobile.text.length<3) {
//        [self.view makeToast:@"请输入正确的号码" duration:1.0 position:CSToastPositionCenter];
//        return;
//    }
//    UIWebView *callWebView = [[UIWebView alloc] init];
//    
//    NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",ad_mobile.text]];
//    
//    [callWebView loadRequest:[NSURLRequest requestWithURL:telURL]];
//    [self.view addSubview:callWebView];
//}
//- (UIView *)erWMView{
//    if (_erWMView == nil) {
//        _erWMView = [[UIView alloc]init];
//        _erWMView.frame = CGRectMake(0, 126, WIDTH, 200);
//        [self.view addSubview:_erWMView];
//        
//        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(20, 10, 80, 80)];
//        if ([_dataDic objectForKey:@"ad_code_img"]) {
//            [image sd_setImageWithURL:[NSURL URLWithString:[_dataDic objectForKey:@"ad_code_img"]]];
//        }else{
//            image.image = [UIImage imageNamed:@"tuwen"];
//        }
//        image.userInteractionEnabled = YES;
//        UITapGestureRecognizer *singleTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonpress:)];
//        [image addGestureRecognizer:singleTap1];
//        image.tag = 91;
//        [_erWMView addSubview:image];
//        
//        UILabel *erwlab = [[UILabel alloc]initWithFrame:CGRectMake(110, 20, WIDTH-140, 25)];
//        erwlab.layer.masksToBounds = YES;
//        erwlab.layer.cornerRadius = 6.0;
//        erwlab.layer.borderWidth = 1;
//        erwlab.layer.borderColor = [UIColorFromRGB(0xefeff1) CGColor];
//        erwlab.text = @" 点击左侧识别或选择二维码";
//        erwlab.font = [UIFont fontWithName:@"Heiti SC" size:12];;
//        erwlab.textColor = UIColorFromRGB(0x666666);
//        [_erWMView addSubview:erwlab];
//        
//        UITextField *desctextview = [[UITextField alloc]initWithFrame:CGRectMake(110, 50, WIDTH-140, 25)];
//        desctextview.tag = 137;
//        desctextview.layer.masksToBounds = YES;
//        desctextview.layer.cornerRadius = 6.0;
//        desctextview.layer.borderWidth = 1;
//        desctextview.layer.borderColor = [UIColorFromRGB(0xefeff1) CGColor];
//        desctextview.placeholder = @" 请输入二维码介绍";
//        
//        if ([_dataDic objectForKey:@"ad_code_title"]) {
//            desctextview.text = [_dataDic objectForKey:@"ad_code_title"];
//        }
//        
//        desctextview.font = [UIFont fontWithName:@"Heiti SC" size:12];;
//        [_erWMView addSubview:desctextview];
//        
//        
//        UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, WIDTH-20, 40)];
//        lab.text = @"注:可上传APP丶个人丶微信公众号二维码,支持扫码关注下载等.";
//        lab.font = [UIFont fontWithName:@"Heiti SC" size:11];;
//        lab.numberOfLines = 0;
//        [_erWMView addSubview:lab];
//    }
//    return _erWMView;
//}

- (IBAction)cutoutButtonClick:(UIButton *)sender {
    
    
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"确认删除" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
    [alert show];
    
    
}
#pragma mark alertview代理
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        
        NSDictionary *dic = @{@"user_token":[user objectForKey:@"token"],
                              @"ads_id":_ads_id};
        
        AFHTTPRequestOperationManager *manager = [MyTools initAFHttpManager];
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        
        [manager POST:DELADS_URL parameters:dic success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            
            [self.view makeToast:[responseObject objectForKey:@"info"] duration:1.0 position:CSToastPositionCenter];
            if ([[responseObject objectForKey:@"status"] intValue] == 1) {
                [self back];
            }
        } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            
            if ([MyTools connectedToNetwork] == 0) {
                [self.view makeToast:@"请检查是否联网" duration:1.0 position:CSToastPositionCenter];
            }else{
                [self.view makeToast:@"服务器错误，再试一次" duration:1.0 position:CSToastPositionCenter];
            }
        }];
    }else if (buttonIndex == 1){
        
    }
    
}
-(void)buttonpress:(UITapGestureRecognizer *)gestureRecognizer{
    
    _action = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"手机拍照",@"本地图片", nil];
    _action.tag = 1;
    [_action showInView:self.view];
}
#pragma mark actionsheet代理，选择相册，选择男女
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (actionSheet == _action) {
        if (buttonIndex == 0) {
            if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
            {
                NSLog(@"支持相机");
                _myPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                [self presentViewController:_myPicker animated:YES completion:nil];
                
            }else{
                NSLog(@"不支持相机");
            }
        }else if (buttonIndex == 1){
            if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
            {
                NSLog(@"支持图库");
            }
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
            {
                NSLog(@"支持相片库");
            }
            _myPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:_myPicker animated:YES completion:nil];
        }else if (buttonIndex == 2){
            //取消
        }
    }
    
}
#pragma mark imagepicker代理
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    //取消选取图片
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"取消选取图片");
    }];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    //选取图片后回调
    NSLog(@"选取图片");
    
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    
    NSData *data;
    
    if ([mediaType isEqualToString:@"public.image"]){
        
        //切忌不可直接使用originImage，因为这是没有经过格式化的图片数据，可能会导致选择的图片颠倒或是失真等现象的发生，从UIImagePickerControllerOriginalImage中的Origin可以看出，很原始，哈哈
        UIImage *originImage = [info objectForKey:UIImagePickerControllerEditedImage];
        //图片压缩，因为原图都是很大的，不必要传原图
        UIImage *scaleImage = [self scaleImage:originImage toScale:0.8];
        
        //以下这两步都是比较耗时的操作，最好开一个HUD提示用户，这样体验会好些，不至于阻塞界面
        if (UIImagePNGRepresentation(scaleImage) == nil) {
            //将图片转换为JPG格式的二进制数据
            data = UIImageJPEGRepresentation(scaleImage, 1);
        } else {
            //将图片转换为PNG格式的二进制数据
            data = UIImagePNGRepresentation(scaleImage);
        }
        
        //将二进制数据生成UIImage
        UIImage *image = [UIImage imageWithData:data];
        
        //判断是否剪切
        if ([_ad_cut isEqualToString:@"1"]&&[_type isEqualToString:@"banner"]) {
            
            //将图片传递给截取界面进行截取并设置回调方法（协议）
            CaptureViewController *captureView = [[CaptureViewController alloc] init];
            captureView.delegate = self;
            captureView.image = image;
            //隐藏UIImagePickerController本身的导航栏
            picker.navigationBar.hidden = YES;
            [picker pushViewController:captureView animated:YES];
        }
        else{
            
            
            
            UIImageView *imageview = (id)[self.view viewWithTag:91];
            imageview.image = image;
            [_myPicker dismissViewControllerAnimated:YES completion:nil];
            //////
            
            NSData *imagedata = UIImageJPEGRepresentation(originImage, 0.5);
            
            NSString * urlstr = [NSString stringWithFormat:@"111,%@",[imagedata base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength]];
            NSDictionary *dic = @{@"img":urlstr};
            AFHTTPRequestOperationManager *manager = [MyTools initAFHttpManager];
            [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            
            [manager POST:shangchuan parameters:dic success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
                [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
                
                if ([[responseObject objectForKey:@"status"] intValue] == 1) {
                    
                    _imageurlstr = [[responseObject objectForKey:@"data"] objectForKey:@"id"];
                }else{
                    [self.view makeToast:[responseObject objectForKey:@"info"] duration:1.0 position:CSToastPositionCenter];
                }
            } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
                [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
                
                if ([MyTools connectedToNetwork] == 0) {
                    [self.view makeToast:@"请检查是否联网" duration:1.0 position:CSToastPositionCenter];
                }else{
                    [self.view makeToast:@"服务器错误，再试一次" duration:1.0 position:CSToastPositionCenter];
                }
            }];
            
        }
    }
    
}
-(void)passImage:(UIImage *)image
{
    
    UIImageView *imageview = (id)[self.view viewWithTag:91];
    imageview.image = image;
    
    NSData *imagedata = UIImageJPEGRepresentation(image, 1);
    
    NSString * urlstr = [NSString stringWithFormat:@"111,%@",[imagedata base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength]];
    NSDictionary *dic = @{@"img":urlstr};
    AFHTTPRequestOperationManager *manager = [MyTools initAFHttpManager];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [manager POST:shangchuan parameters:dic success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        if ([[responseObject objectForKey:@"status"] intValue] == 1) {
            
            _imageurlstr = [[responseObject objectForKey:@"data"] objectForKey:@"id"];
        }else{
            [self.view makeToast:[responseObject objectForKey:@"info"] duration:1.0 position:CSToastPositionCenter];
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        if ([MyTools connectedToNetwork] == 0) {
            [self.view makeToast:@"请检查是否联网" duration:1.0 position:CSToastPositionCenter];
        }else{
            [self.view makeToast:@"服务器错误，再试一次" duration:1.0 position:CSToastPositionCenter];
        }
    }];
}
#pragma mark- 缩放图片
-(UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize
{
    UIGraphicsBeginImageContext(CGSizeMake(image.size.width*scaleSize,image.size.height*scaleSize));
    [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height *scaleSize)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    for (int i = 130; i<138; i++) {
        UITextField *textview = (id)[self.view viewWithTag:i];
        [textview resignFirstResponder];
    }
    [_paiXutextfiled resignFirstResponder];
}

-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.tabBarController.tabBar.translucent = YES;
    self.tabBarController.tabBar.hidden = YES;
}

@end
